package parser;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import properties.Property;

/**
 *
 * @author fabio
 */
public class MainDebug {

    public static void main(String[] args) {
        String map40 = "/home/fabio/prova.ctl";
        String input = "";
        try {
            input = readFile(map40);
        } catch (IOException ex) {
            System.out.println("File non trovato " + ex);
            System.exit(1);
        }
        CharStream cs = new ANTLRInputStream(input);
        Lexer lexer = new mccpropertyLexer(cs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        mccpropertyParser parser = new mccpropertyParser(tokens);
        ParseTree tree = parser.property_set();
        ParseTreeWalker chuck = new ParseTreeWalker();
        Translator listener = new Translator(parser,true);
        chuck.walk(listener, tree);
        for(Property p : listener.getProperties()){
            System.out.println(p.getName()+":"+p.getTranslation());
        }
        /*
        
        
        
        //System.out.println((parser.getTokenStream().getText()));
        mccpropertyBaseVisitor visitor = new mccpropertyBaseVisitor();
        visitor.visit(parser.property_set());*/


    }

    private static String readFile(String path) throws IOException {
        try (FileInputStream stream = new FileInputStream(new File(path))) {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            /*
             * Instead of using default, pass in a decoder.
             */
            return Charset.defaultCharset().decode(bb).toString();
        }
    }
}
